package com.atguigu.tingshu.common.login;

import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.data.domain.Auditable;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.ArrayList;

@Component
@Aspect
public class TsLoginAspect {

    @Around("execution(* com.atguigu.tingshu.*.api.*.*(..)) && @annotation(tsLogin)")
    public Object tsLogin(ProceedingJoinPoint joinPoint,TsLogin tsLogin) throws Throwable{
        //判断是否需要登录
        if(tsLogin.required()){
            //需要登录
            //从Spring容器上下文中获取对象
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            ServletRequestAttributes servletRequestAttributes=(ServletRequestAttributes) requestAttributes;
            HttpServletRequest request = servletRequestAttributes.getRequest();
            String token = request.getHeader("token");

            //判断token是否为空
            if(!StringUtils.hasText(token)){
                throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
            }

        }

        return joinPoint.proceed();

    }
}
